package com.sibbay.vta.adaptor;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sibbay.vta.R;

public final class StationListAdapter extends BaseAdapter {
    
	private final Context mContext;
	private final int mSelectedColor;
	private final String[] mStationList;
	private final boolean[] mStationListSelected;
	private int mStationListSelectedPos = 0;
	private Drawable mDefBackground;

    public StationListAdapter(final Context context, final String[] stationList) {
    	mContext = context;
    	mStationList = stationList;
    	mStationListSelected = new boolean[stationList.length];
    	mSelectedColor = mContext.getResources().getColor(R.color.station_selected);
    }

    @Override
    public int getCount() {
        return mStationList.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	final ViewGroup tg;
        if (convertView == null) {
            tg = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.route_list_item, parent, false);
            if (mDefBackground == null) {
            	mDefBackground = tg.getBackground();
            }
        } else {
            tg = (ViewGroup) convertView;
        }
        ((TextView)tg.findViewById(R.id.text)).setText(mStationList[position]);
        if (mStationListSelected[position] == true) {
        	tg.setBackgroundColor(mSelectedColor);
//        	tg.findViewById(R.id.status).setVisibility(View.VISIBLE);
        } else {
        	tg.setBackgroundDrawable(mDefBackground);
//        	tg.getB setBackgroundColor(Color.TRANSPARENT);
//        	tg.findViewById(R.id.status).setVisibility(View.INVISIBLE);
        }
        return tg;
    }
    
    public void setSelected(int pos) {
    	mStationListSelected[mStationListSelectedPos] = false;
    	mStationListSelectedPos = pos;
    	mStationListSelected[mStationListSelectedPos] = true;
    	notifyDataSetChanged();
    }
    
    public int getSelected() {
    	return mStationListSelectedPos;
    }
}
